<template>
<div class="login">
    <header>
      <img src="@/assets/images/登录页logo@2x.png" alt="" />
    </header>
    <div class="login-page">
      <el-card class="box-card">
        <div slot="header" class="clearfix title">
          <span>后台管理系统</span>
          <a href="javascript:;" @click="toRegister">去注册</a>
        </div>
        <div class="text item">
          <el-form :model="form" :rules="rules" ref="form">
            <el-form-item prop="username">
              <div class="text">用户名</div>
              <el-input type="text" placeholder="请输入用户名" v-model="form.username"/>
            </el-form-item>
            <el-form-item prop="password">
              <div class="text">密码</div>
              <el-input type="password" placeholder="请输入密码" v-model="form.password"/>
            </el-form-item>
            <el-form-item>
                <el-button type="primary" class="btn" @click="login">登录</el-button>
            </el-form-item>
          </el-form>
        </div>
      </el-card>
    </div>
  </div>
</template>

<script>
export default {
  data () {
    return {
      form: {
        username: '',
        password: ''
      },
      rules: {
        username: [
          { required: true, message: '请输入用户名', trigger: 'blur' },
          { min: 5, max: 11, message: '长度在 5 到 11 个字符', trigger: 'blur' }
        ],
        password: [
          { required: true, message: '请输入密码', trigger: 'blur' },
          { min: 5, max: 11, message: '长度在 5 到 11 个字符', trigger: 'blur' }
        ]
      }
    }
  },
  methods: {
    toRegister () {
      this.$router.push('/register')
    },
    // 用户登录
    async login () {
      try {
        this.$refs.form.validate()
        // 调用actions请求接口
        await this.$store.dispatch('user/login', this.form)
        // 登陆成功后的弹窗
        this.$message.success('登陆成功')
        // 跳转到首页
        this.$router.push('/')
      } catch (e) {
        console.log(e)
      }
    }
  }
}
</script>

<style lang="less" scoped>
header {
  margin-top: 30px;
  img {
    margin-left: 50px;
    width: 110px;
    height: 48px;
  }
}
.login-page {
  display: flex;
  justify-content: center;
  align-items: center;
  min-height: 90vh;
  background: url(@/assets/images/登录注册bg@2x.png) no-repeat center / cover;
  .box-card {
    width: 540px;
    height: 460px;
    .title {
      display: flex;
      flex-direction: column;
      a {
        text-decoration: none;
        font-size: 16px;
        color: #1e5eff;
        text-align: center;
        &:hover {
          color: #4f81ff;
        }
      }
      span {
        font-size: 32px;
        color: #131523;
        letter-spacing: 0;
        text-align: center;
        line-height: 44px;
        font-weight: 400;
      }
    }
    .item {
        margin-top: 40px;
        display: flex;
        justify-content: center;
      .text {
        font-size: 14px;
        color: #5a607f;
      }
      .btn {
        background: #4F81FF;
        width: 420px;
        height: 48px;
      }
    }
  }
}
</style>
